﻿#include "tool.h"
#include "string.h"
void tool_string_insert(char* srcStr,
                        const unsigned short index,
                        const char* str)//在字符串srcStr的index位置插入字符串str
{
  unsigned short temp=0;
  unsigned short srcStrLen=strlen(srcStr);
  unsigned short strLen=strlen(str);
  for(temp=srcStrLen-1;temp>=index;--temp)
    {
      srcStr[temp+strLen]=srcStr[temp];
    }
  for(temp=0;temp<strLen;++temp)
    {
      srcStr[index+temp]=str[temp];
    }
}

BOOL tool_string_equal(const char str_1[],
                       const char str_2[])//判断两字符串是否相等
{
  const unsigned short len=strlen(str_1);
  if(len!=strlen(str_2))
    {
      return FALSE;
    }
  return tool_string_equal_len(str_1,str_2,len);
}

BOOL tool_string_equal_len(const char str_1[],
                           const char str_2[],
                           const unsigned short len)//在len长度内两字符串是否一样
{
  unsigned short temp=0;
  for(temp=0;temp<len;++temp)
    {
      if(str_1[temp]!=str_2[temp])
        {
          return FALSE;
        }
    }
  return TRUE;
}

short tool_string_indexOf(const char srcStr[],
                          const unsigned short srcStrLen,
                          const char tarStr[],
                          const unsigned short tarStrLen)//得到tarStr在srcStr中第一次出现的下标
{
  unsigned short nowAt=0;
  for(;TRUE;++nowAt)
    {
      if(tarStrLen+nowAt>srcStrLen)
        {
          return -1;
        }
      if(tool_string_equal_len(srcStr+nowAt,tarStr,tarStrLen))
        {
          return nowAt;
        }
    }
}

void tool_string_split_index(const char srcStr[],
                             const char spiltFlag,
                             const unsigned char index,
                             char tarStr[])//根据spiltFlag分割出下标为index的子字符串，并将结果存入tarStr
{
  unsigned short nowAt_srcStr = 0;
  unsigned char nowAt_index = 0;
  unsigned short nowAt_tarStr=0;
  const unsigned short srcStrLen=strlen(srcStr);
  for(nowAt_srcStr=0;TRUE;++nowAt_srcStr)
    {
      if(nowAt_srcStr>=srcStrLen)
        {
          break;
        }
      if(spiltFlag==srcStr[nowAt_srcStr])
        {
          ++nowAt_index;
          continue;
        }
      if(index>nowAt_index)
        {
          continue;
        }
      else if(index<nowAt_index)
        {
          break;
        }
      else
        {
          tarStr[nowAt_tarStr++]=srcStr[nowAt_srcStr];
        }
    }
}

unsigned short tool_string_split(const char srcStr[],
                                 const char spiltFlag,
                                 const unsigned short tarStrArrDep,
                                 const unsigned short tarStrArrLen,
                                 char* tarStrArr)//根据spiltFlag分割srcStr结果存储在tarStrArr数组中
{
  unsigned short nowAt_srcStr = 0;
  unsigned short nowAt_tarStrLen=2;
  unsigned short nowAt_tarStrDep=0;
  const unsigned short srcStrLen=strlen(srcStr);
  for(nowAt_srcStr=0;TRUE;++nowAt_srcStr)
    {
      if(nowAt_srcStr>=srcStrLen)
        {
          break;
        }
      if(spiltFlag==srcStr[nowAt_srcStr])
        {
          nowAt_tarStrLen=0;
          if(nowAt_tarStrDep+1<tarStrArrDep)
            {
              ++nowAt_tarStrDep;
            }
          else
            {
              break;
            }
        }
      else
        {
          tarStrArr[nowAt_tarStrDep*tarStrArrLen+nowAt_tarStrLen]=srcStr[nowAt_srcStr];
          if(nowAt_tarStrLen+1<tarStrArrLen)
            {
              ++nowAt_tarStrLen;
            }
          else
            {
              continue;
            }
        }
    }
  return nowAt_tarStrDep+1;
}

void tool_byteArray_copy(char* srcStr,
                      const char* copiedStr,
                      const unsigned short copiedStrLen)//字节序列复制到新的序列
{
    unsigned short i=0;
    for(i=0;i<copiedStrLen;++i)
      {
        srcStr[i]=copiedStr[i];
      }
}

char* tool_byteArray_append(char* srcStr,
                         const unsigned short srcLen,
                         const char* addedStr,
                         const unsigned short addedStrLen)//在字节序列srcStr的srcLen后添加新的字节序列
{
  unsigned short i=0;
  for(i=0;i<addedStrLen;++i)
    {
      srcStr[srcLen+i]=addedStr[i];
    }
  return srcStr;
}

unsigned short tool_twoCharToShort(const char char_h,
                                   const char char_l)//两个char型转short型
{
  return ((char_h<<8)&0xff00)|(char_l&0x00ff);
}

void tool_ShortToTwoChar(const short value,
                         char* char_h,
                         char* char_l)//short类型转两个char型
{
  *char_h=(value>>8)&0xff;
  *char_l=(value&0xff);
}

char charToHexChar1(const char srcChar)//16进制字节转16进制的字符
{
  return srcChar > 0x09 ? srcChar + 0x37 : srcChar + 0x30;
}

void tool_string_charToHexChar(char srcChar,
                               char* tarChar_h,
                               char* tarChar_l)//一个十六进制数据转两个十六进制的字符
{
  *tarChar_h=charToHexChar1((srcChar>>4)&0x0f);
  *tarChar_l=charToHexChar1(srcChar&0x0f);
}

void tool_string_byteArrToHexStr(const char* srcStr,
                                 const unsigned short len,
                                 char* tarStr)//16进制字节序列转16进制的字符串
{
  unsigned short i=0;
  for(i=0;i<len;++i)
    {
      tool_string_charToHexChar(*(srcStr+i),tarStr+2*i,tarStr+2*i+1);
    }
}

unsigned char hexCharToChar1(const unsigned char data_h,
                             const unsigned char data_l)//16进制字符转为16进制字节
{
  return ((data_h > 0x40 ? data_h - 0x37 : data_h - 0x30) << 4) | (data_l > 0x40 ? data_l - 0x37 : data_l - 0x30);
}

void tool_string_hexCharToChar(char srcChar_h,
                               char srcChar_l,
                               char* tarChar)//两个字符拼接为一个字节
{
  *tarChar=hexCharToChar1(srcChar_h,srcChar_l);
}

void tool_string_hexStrToByteArr(const char* srcHexStr,
                                 char* tarByteArr)//16进制的字符串转16进制字节序列
{
  unsigned short i=0;
  unsigned short len=strlen(srcHexStr);
  for(i=0;i<len;i+=2)
    {
      tool_string_hexCharToChar(*(srcHexStr+i),*(srcHexStr+i+1),tarByteArr+i/2);
    }
}

BOOL tool_string_numToStr(const short num,
                          char* str)//数字转字符串
{
  short num1=num>0?num:-num;
  unsigned char nowAt=0;
  unsigned char nowAt1=0;
  char temp[10];
  memset(temp,0,10);
  while (TRUE)
    {
      if(num1/10)
        {
          temp[nowAt++]=charToHexChar1(num1%10);
          num1/=10;
        }
      else
        {
          temp[nowAt++]=charToHexChar1(num1%10);
          if(num<0)
            {
              temp[nowAt++]='-';
              num1=-num1;
            }
          for(nowAt1=0;nowAt1<nowAt;++nowAt1)
            {
              str[nowAt1]=temp[nowAt-1-nowAt1];
            }
          return TRUE;
        }
    }
}


BOOL tool_string_strToNum(const char* str,
                          unsigned short* num)//字符串转数字
{
  short num1=0;
  unsigned short multiples=1;
  unsigned char index=strlen(str)-1;
  for(;0<index;--index)
    {
      if('0'<=str[index]&&'9'>=str[index])
        {
          num1+=hexCharToChar1(0,str[index])*multiples;
          multiples*=10;
        }
      else
        {
          return FALSE;
        }
    }
  if('-'==str[0])
    {
      *num=-num1;
      return TRUE;
    }
  else if('0'<=str[0]&&'9'>=str[0])
    {

      num1+=hexCharToChar1(0,str[0])*multiples;
      *num=num1;
      return TRUE;
    }
  return FALSE;
}

